﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module;
using System.Web.UI.HtmlControls;
using BLL.Common;

namespace App
{
    public partial class FamousRegion_FamousDynamicPaperList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                pageInit();
            }
        }

        protected void rptDynamicList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                PaperInfo p = e.Item.DataItem as PaperInfo;
                HtmlAnchor link = e.Item.FindControl("lb_Dynamic") as HtmlAnchor;

                if (p != null && p.PaperId > 0 && link != null)
                {
                    link.InnerText = p.Subject + " (" + p.DateCreated.ToShortDateString() + ")";
                    link.HRef = Contexts.AppPath + "/FamousRegion/FamousDynamicPaper.aspx?" + Contexts.PaperId + "=" + Contexts.GetEncrypt(p.PaperId.ToString()) + "&" + Contexts.RegionCategoryId + "=" + Contexts.GetEncrypt(this.hf_categoryId.Value);
                }

            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                if (rptDynamicList.Items.Count <= 0)
                {
                    ((Label)e.Item.FindControl("lblNoRecord")).Visible = true;
                }
            }
        }

        protected void anpOrders_PageChanged(object sender, EventArgs e)
        {
            pageInit();
        }

        private void pageInit()
        {

            List<PaperInfo> Pages = getPages();
            if (Pages != null)
            {

                this.rptDynamicList.DataSource = Pages;
                this.rptDynamicList.DataBind();
            }
        }

        private List<PaperInfo> getPages()
        {
            long categoryId = Contexts.GetAPPRegionCategoryID("/Index.aspx");

            this.hf_categoryId.Value = categoryId.ToString();

            List<PaperInfo> pages = BLL.BPaper.getInstance().GetAllRegionCategoryPage(categoryId);

            if (pages != null && pages.Count > 0)
            {
                this.anpOrders.RecordCount = pages.Count;
                List<PaperInfo> tempPages = new List<PaperInfo>();
                tempPages.AddRange(pages);
                int currentNum = 0;

                //分页数据处理
                foreach (PaperInfo info in tempPages)
                {
                    currentNum++;
                    if (currentNum < anpOrders.PageSize * (anpOrders.CurrentPageIndex - 1) + 1 || currentNum > anpOrders.PageSize * anpOrders.CurrentPageIndex)
                    {
                        pages.Remove(info);
                    }
                }
            }

            return pages;
        }
    }
}